package com.example.electricbicycle.controller;

import com.example.electricbicycle.po.User;
import com.example.electricbicycle.service.UserService;
import com.example.electricbicycle.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/UserListServlet")
public class UserListServlet extends HttpServlet {
    private final UserService userService = new UserServiceImpl();
    private static final int DEFAULT_PAGE_SIZE = 10;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 分页参数处理
        int currentPage = 1;
        int pageSize = DEFAULT_PAGE_SIZE;

        try {
            currentPage = Integer.parseInt(request.getParameter("page"));
        } catch (NumberFormatException e) {
            // 使用默认值
        }

        // 获取用户列表和总记录数
        List<User> userList = userService.getAllUsers(currentPage, pageSize);
        int totalCount = userService.getUserCount();
        int totalPages = (int) Math.ceil((double) totalCount / pageSize);

        // 获取所有用户数据（不分页）
        List<User> allUsers = userService.getAllUsers(1, 10000);

        // 计算角色统计值
        int adminCount = userService.getAdminCount();
        int salesCount = userService.getSalesCount();
        int warehouseCount = userService.getWarehouseCount();
        int salesAdminCount = userService.getSalesAdminCount();
        int afterSalesCount = userService.getAfterSalesCount();

        // 属性设置
        request.setAttribute("userList", userList);
        request.setAttribute("allUsers", allUsers); // 添加所有用户数据
        request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalPages", totalPages);
        request.setAttribute("totalCount", totalCount);

        // 添加角色统计属性
        request.setAttribute("adminCount", adminCount);
        request.setAttribute("salesCount", salesCount);
        request.setAttribute("warehouseCount", warehouseCount);
        request.setAttribute("salesAdminCount", salesAdminCount);
        request.setAttribute("afterSalesCount", afterSalesCount);

        // 从session获取消息并设置到request
        HttpSession session = request.getSession();
        String successMsg = (String) session.getAttribute("successMsg");
        String errorMsg = (String) session.getAttribute("errorMsg");

        if (successMsg != null) {
            request.setAttribute("successMsg", successMsg);
            // 清除session中的消息，避免重复显示
            session.removeAttribute("successMsg");
        }
        if (errorMsg != null) {
            request.setAttribute("errorMsg", errorMsg);
            // 清除session中的消息，避免重复显示
            session.removeAttribute("errorMsg");
        }

        // 转发到管理员页面
        request.getRequestDispatcher("/admin/userlist.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}